// pages/search/search.js
const HTTP = require('../../utils/util');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchValue:'',
        historyList:[],
        shopList:[],
        active:1,
        keywords:'',
        shopPage:1,
        shopPageNumber:10
    },
    // 监听取消
    onCancel(){
        this.setData({
            keywords:''
        });
    },
    // 监听keywords变化
    onSearch(e){
        this.setData({
            keywords:e.detail
        });
    },
    //处理点击历史记录
    handleHistory(e){
        this.setData({
            keyword:e.target.dataset.keywords
        });
        this.getSearch();
    },
    // 点击tab变化
    changeState(e){
        let id = Number(e.target.dataset.id);
        if(id === 1){
            this.setData({
                active:1,
                shopPage:1
            });
            this.getSearch();
        }else if(id === 2){
            this.setData({
                active:2,
                shopPage:1
            });
            this.getSearch();
        }else{
            this.setData({
                active:3,
                shopPage:1
            });
            this.getSearch();
        }
    },
    //处理搜索
    handleSearch(e){
        this.setData({
            keywords:e.detail
        });
        this.getSearch();
    },
    //搜索
    getSearch(e){
        HTTP.getData('/api/index/search',{
            words:this.data.keywords,
            type:this.data.active,
            limit:this.data.shopPageNumber,
            page:this.data.shopPage
        },res=>{
            this.howManyLoad(this.data.shopPage,res.data.data);
        });
    },
    // 判断加载多少店铺
    howManyLoad(page,res){
        if(page === 1){
            if(res.length === 0){
                wx.showToast({
                    title:"没有更多商家啦~",
                    duration:500,
                    icon:'none'
                });
            }
            this.setData({
                shopList:res
            })
        }else{
            if(res.length === 0){
                wx.showToast({
                    title:"没有更多商家啦~",
                    duration:500,
                    icon:'none'
                });
            }
            let newShop = [];
            newShop = this.data.shopList;
            res.map(item=>{
                newShop.push(item);
            })
            this.setData({
                shopList: newShop
            });
        }
    },
    //上拉加载
    onReachBottom(e){
        console.log(1231231)
        this.setData({
            shopPage: this.data.shopPage+1
        });
        this.getSearch();
    },
    // 获取搜索记录
    getHistory(){
        HTTP.getData('/api/index/searchHis',{},res=>{
            this.setData({
                historyList:res.data
            });
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getHistory();
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    searchValue(e){

    },

    onCancel(){
        this.setData({
            searchValue:''
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})